package org.example.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.DAO.StudentDao;
import org.example.User.Student;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {

        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new
        SqlSessionFactoryBuilder().build(inputStream);
        //System.out.println(sqlSessionFactory);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        System.out.println(sqlSession);


        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
//        //改
//        int rows = mapper.update(new Student(1,"MXX"));
//        System.out.println(rows);
//
//        //查
//        List<Student> res=mapper.selectAllStudents();
//        for(Student student:res)
//        {
//            System.out.println(student.getId()+" "+student.getName());
//        }
//
//        //删
//        int row2=mapper.delete(1);
//        System.out.println(row2);

        //插入
        Student student=new Student(1,"mxx");
        int row3=mapper.insert(student);
        System.out.println(row3);

        sqlSession.close();

    }
}
